package lambda.utils;

import lambda.function.BranchHandle;
import lambda.function.PresentOrElseHandler;
import lambda.function.ThrowExceptionFunction;

/**
 * 抛异常工具类
 *
 * @author miweidong
 * @date 2021-12-03 14:00
 */
public class VUtils {

    /**
     * 如果为true，抛出异常
     *
     * @param b 标识
     * @return ThrowExceptionFunction对象
     */
    public static ThrowExceptionFunction isTrue(boolean b) {
        return (message -> {
            if (b) {
                throw new RuntimeException(message);
            }
        });
    }

    /**
     * 分支操作
     *
     * @param b 分支标识
     * @return BranchHandle
     */
    public static BranchHandle isTrueOrFalse(boolean b) {
        return ((trueHandle, falseHandle) -> {
            if (b) {
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        });
    }

    /**
     * 判空操作
     *
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> PresentOrElseHandler<T> isNullOrNonNull(T obj) {
        return (action, emptyAction) -> {
            if (null != obj) {
                action.accept(obj);
            } else {
                emptyAction.run();
            }
        };
    }
}
